<?php

namespace YMApi\Repositories;
use YMApi\Model\GoodsModel;
use YMApi\Model\GoodspreselltimeModel;
use YMApi\Model\GoodsstorageModel;
use YMApi\Model\GoodsWidModel;

/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/28
 * Time: 9:47
 */
class GoodsRepository
{
	/**
	 * @var GoodsWidModel
	 */
	protected $goodsWidModel;

	/**
	 * @var
	 */
	protected $userId;

	/**
	 * @var
	 */
	protected $wid;

	/**
	 * @var GoodsstorageModel
	 */
	protected $goodsStorageModel;

	/**
	 * @var GoodsModel
	 */
	protected $goodsModel;

	/**
	 * @var BuyNowGoodsRepository
	 */
	protected $buyNowGoodsRepository;

	/**
	 * @var GoodspreselltimeModel
	 */
	protected $goodsPresellTimeModel;

	/**
	 * 注入实例
	 * GoodsRepository constructor.
	 * @param $userId
	 * @param $wid
	 */
	public function __construct($userId, $wid)
	{
		$this->goodsWidModel = new GoodsWidModel();
		$this->goodsStorageModel = new GoodsstorageModel();
		$this->goodsModel = new GoodsModel();
		$this->buyNowGoodsRepository = new BuyNowGoodsRepository($userId, $wid);
		$this->goodsPresellTimeModel = new GoodspreselltimeModel();

		$userId = empty($userId) ? 0 : $userId;
		$wid = empty($wid) ? 8 : $wid;

		$this->userId = $userId;
		$this->wid = $wid;
	}


	/**
	 * Created by Jerry
	 * @param $goods
	 * @return mixed
	 */
	public function formatGoodsByTemplate($goods)
	{
		foreach ($goods as $key => $value){

			$goodsInfo = $this->findGoodsInfoById($value['goods_id']);

			if (empty($goodsInfo)) {
				unset($goods[$key]);
				continue;
			}

			$goodsNumber = $this->findGoodsStorageById($value['goods_id']);

			$goods[$key]['spec_unit'] = $goodsInfo['spec_unit'];
			$goods[$key]['spec_value'] = $goodsInfo['spec_value'];
			$goods[$key]['min_buy_num'] = $goodsInfo['min_buy_num'];
			$goods[$key]['goods_name'] = $goodsInfo['goods_name'];
			$goods[$key]['original_img'] = img_init($goodsInfo['original_img']);
			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

			if($goodsInfo['goods_img']){
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_img']);
			}else{
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_thumb']);
			}
			$goods[$key]['goods_img'] =  img_init($goodsInfo['goods_img']);

			$goods[$key]['goods_brief'] = $goodsInfo['goods_brief'];
			$goods[$key]['is_presell'] = ($goodsInfo['is_presell'] == 1) ? $this->goodsStorageModel->check_whether_presell($value['goods_id'], $this->wid) : $goodsInfo['is_presell'];

			if ($goodsInfo['is_presell'] == 1) {
//				预售商品使用虚拟库存
				$goods[$key]['goods_number'] = $goodsInfo['goods_number'];
			}
			else
			{
//				现售商品使用实际库存
				$goods[$key]['goods_number'] = $goodsNumber;
			}

			if ($goods[$key]['is_presell']) {
				if ($goodsInfo['delivery_type'] == 1) {
					if ($this->goodsPresellTimeModel->check_shipping_goodspreselltime($value['goods_id'])) {
						$time = time();
						$shippingTime = $this->goodsPresellTimeModel->check_shippingtime($value['goods_id'], $time);

						if ($shippingTime) {
							$goods[$key]['preselltimeres'] = $shippingTime;
						}
						else{
							$goods[$key]['preselltimeres'] = array(
								'delivery_time'	=>	'',
							);
						}
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else if($goodsInfo['delivery_type'] == 2) {
					if ($goodsInfo['delivery_cycle']) {
						$gmd = local_date('Y-m-d', gmtime());

						$goodsPresellTime = local_date('Y-m-d H:i:s', local_strtotime($gmd . ' +' . $goodsInfo['delivery_cycle'] . ' day ' . C('DEFAULT_PRESELL_TIME_H')));
						$goods[$key]['preselltimeres'] = array(
							'delivery_time' => $goodsPresellTime
						);
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else{
					$goods[$key]['preselltimeres'] = array(
						'delivery_time'	=>	'',
					);
				}
			}
			else{
				$goodsStorage = $this->goodsStorageModel->get_warehous_goods($this->wid, $value['goods_id']);
				if ($goodsStorage) {
					$goods[$key]['goods_number'] = $goodsStorage['practical_num'];
				}
				else{
					$goods[$key]['goods_number'] = 0;
				}
				$goods[$key]['preselltimeres'] = array('delivery_time' => '');
			}

			$goods[$key]['warn_number'] = $goodsInfo['warn_number'];

			if ($goods[$key]['goods_number'] == 0) {
				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
			}

//			else if ($goodsInfo['is_presell'] == 1 && $goodsInfo['goods_number'] <= 0) {
//				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
//			}

//			登录已未登录商品价格
			if ($this->userId) {
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price($value['goods_id'], $this->userId);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}
			else
			{
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}

//			促销价处理
			$price = bargain_price($goodsInfo['promote_price'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
			if ($goodsInfo['is_buynow'] == 0) {
				$goods[$key]['promote_end_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_end_date']));
				$goods[$key]['promote_start_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_start_date']));
				$goods[$key]['promote_price'] = $price;
			}
//			else
//			{
//				$goods[$key]['promote_price'] = 0;
//				$goods[$key]['promote_end_date'] = '';
//				$goods[$key]['promote_start_date'] = '';
//			}

			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

//			秒杀
			if ($goodsInfo['is_buynow'] == 1) {
				$goods[$key]['promote_end_date'] = $goodsInfo['promote_end_date'];
				$goods[$key]['promote_start_date'] = $goodsInfo['promote_start_date'];
				$goods[$key]['promote_goods_number'] = $goodsInfo['promote_goods_number'];
				$goods[$key]['promote_today_number'] = $goodsInfo['promote_today_number'];
				$goods[$key]['promote_price'] = $price;

				$goods[$key] = $this->buyNowGoodsRepository->getBuyNowGoodsData($goods[$key]);
			}

		}

		return $goods;

	}


	/**
	 * Created by Jerry
	 * @param $goods
	 * @return mixed
	 */
	public function formatCategoryGoodsByTemplate($goods)
	{
		foreach ($goods as $key => $value){

			$goodsInfo = $this->findGoodsInfoById($value['goods_id']);

			if (empty($goodsInfo)) {
				unset($goods[$key]);
				continue;
			}

			$goodsNumber = $this->findGoodsStorageById($value['goods_id']);

			$goods[$key]['spec_unit'] = $goodsInfo['spec_unit'];
			$goods[$key]['spec_value'] = $goodsInfo['spec_value'];
			$goods[$key]['min_buy_num'] = $goodsInfo['min_buy_num'];
			$goods[$key]['goods_name'] = $goodsInfo['goods_name'];
			$goods[$key]['original_img'] = img_init($goodsInfo['original_img']);
			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

			if($goodsInfo['goods_img']){
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_img']);
			}else{
				$goods[$key]['goods_thumb'] = img_init($goodsInfo['goods_thumb']);
			}
			$goods[$key]['goods_img'] =  img_init($goodsInfo['goods_img']);

			$goods[$key]['goods_brief'] = $goodsInfo['goods_brief'];
			$goods[$key]['is_presell'] = ($goodsInfo['is_presell'] == 1) ? $this->goodsStorageModel->check_whether_presell($value['goods_id'], $this->wid) : $goodsInfo['is_presell'];

			if ($goodsInfo['is_presell'] == 1) {
//				预售商品使用虚拟库存
				$goods[$key]['goods_number'] = $goodsInfo['goods_number'];
			}
			else
			{
//				现售商品使用实际库存
				$goods[$key]['goods_number'] = $goodsNumber;
			}

			if ($goods[$key]['is_presell']) {
				if ($goodsInfo['delivery_type'] == 1) {
					if ($this->goodsPresellTimeModel->check_shipping_goodspreselltime($value['goods_id'])) {
						$time = time();
						$shippingTime = $this->goodsPresellTimeModel->check_shippingtime($value['goods_id'], $time);

						if ($shippingTime) {
							$goods[$key]['preselltimeres'] = $shippingTime;
						}
						else{
							$goods[$key]['preselltimeres'] = array(
								'delivery_time'	=>	'',
							);
						}
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else if($goodsInfo['delivery_type'] == 2) {
					if ($goodsInfo['delivery_cycle']) {
						$gmd = local_date('Y-m-d', gmtime());

						$goodsPresellTime = local_date('Y-m-d H:i:s', local_strtotime($gmd . ' +' . $goodsInfo['delivery_cycle'] . ' day ' . C('DEFAULT_PRESELL_TIME_H')));
						$goods[$key]['preselltimeres'] = array(
							'delivery_time' => $goodsPresellTime
						);
					}
					else{
						$goods[$key]['preselltimeres'] = array(
							'delivery_time'	=>	'',
						);
					}
				}
				else{
					$goods[$key]['preselltimeres'] = array(
						'delivery_time'	=>	'',
					);
				}
			}
			else{
				$goodsStorage = $this->goodsStorageModel->get_warehous_goods($this->wid, $value['goods_id']);
				if ($goodsStorage) {
					$goods[$key]['goods_number'] = $goodsStorage['practical_num'];
				}
				else{
					$goods[$key]['goods_number'] = 0;
				}
				$goods[$key]['preselltimeres'] = array('delivery_time' => '');
			}

			$goods[$key]['warn_number'] = $goodsInfo['warn_number'];

			if ($goods[$key]['goods_number'] == 0) {
				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
			}

//			else if ($goodsInfo['is_presell'] == 1 && $goodsInfo['goods_number'] <= 0) {
//				$goods[$key]['is_book_goods'] = $this->goodsBook($value['goods_id']);
//			}

//			登录已未登录商品价格
			if ($this->userId) {
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price($value['goods_id'], $this->userId);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}
			else
			{
				$goods[$key]['market_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id'], 1);
				$goods[$key]['true_vip_price'] = $this->goodsModel->get_vip_price_for_all($value['goods_id']);
			}

//			促销价处理
			$price = bargain_price($goodsInfo['promote_price'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
			if ($goodsInfo['is_buynow'] == 0 && $price > 0) {
				$goods[$key]['promote_end_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_end_date']));
				$goods[$key]['promote_start_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_start_date']));
				$goods[$key]['promote_price'] = $price;
			}

			if ($goodsInfo['is_buynow'] == 0 && $price == 0) {
				$goods[$key]['promote_end_date'] = 0;
				$goods[$key]['promote_start_date'] = 0;
				$goods[$key]['promote_price'] = $price;
			}



			$goods[$key]['is_buynow'] = $goodsInfo['is_buynow'];

//			秒杀
			if ($goodsInfo['is_buynow'] == 1) {

				$goods[$key]['promote_end_date'] = $goodsInfo['promote_end_date'];
				$goods[$key]['promote_start_date'] = $goodsInfo['promote_start_date'];
				$goods[$key]['promote_goods_number'] = $goodsInfo['promote_goods_number'];
				$goods[$key]['promote_today_number'] = $goodsInfo['promote_today_number'];
				$goods[$key]['promote_price'] = $goodsInfo['promote_price'];

				$goods[$key] = $this->buyNowGoodsRepository->getBuyNowGoodsData($goods[$key]);
			}

		}

		return $goods;

	}

	/**
	 * Created by Jerry
	 * @param $category_id
	 * @param $goods_where
	 * @return bool|mixed
	 */
	public function findCategoryGoodsByCategoryId($category_id, $goods_where)
	{
		return $this->goodsModel->findCategoryGoodsByCategoryId($category_id, $goods_where);
	}

	/**
	 * 返回商品详情
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	protected function findGoodsInfoById($id)
	{
		return $this->goodsWidModel->setTableNameByWid($this->wid)->get_goods_info_by_goods_id($id);
	}

	/**
	 * 返回指定id库存数量
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	protected function findGoodsStorageById($id)
	{
		$data = $this->goodsStorageModel->get_warehous_goods($this->wid, $id);

		return $data['practical_num'] + 0;
	}

	/**
	 * 返回订阅数据
	 * Created by Jerry
	 * @param $goodsId
	 * @return int
	 */
	protected function goodsBook($goodsId)
	{
		$rs = $this->goodsModel->is_book_goods($goodsId, $this->userId, 0);

		if ($rs) {
			return 1;
		}

		return 0;
	}
}